/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl.methods;

import java.util.UUID;

import org.dctmcommons.DctmCommons;
import org.dctmcommons.ISessionCallback;

import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.IDfDocument;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfLogger;

import static com.documentum.fc.common.DfDocbaseConstants.*;

/**
 * Dummy method that's used to try out method-related code.
 * @author Mark Davidson
 */
public class DummyMethod extends AbstractServerMethod {

	/** Carriage return constant. */
	private static final String CR = "\n";

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.impl.methods.AbstractServerMethod#onExecute()
	 */
	@Override
	public int onExecute() throws Exception {
		createDocument(createContent());
		return STATUS_SUCCESS;
	}

	/**
	 * Creates document content.
	 * @return The content as a string
	 */
	private String createContent() {
		final StringBuilder sb = new StringBuilder();
		sb.append("Username=" + getUsername() + CR);
		sb.append("Ticket=" + getTicket() + CR);
		sb.append("Repository=" + getRepositoryName() + CR);
		sb.append("Trace Level=" + getTraceLevel() +CR);
		sb.append("Message=" + getParamValue("message", "Value not defined"));
		return sb.toString();
	}

	/**
	 * Creates a document in the repository.
	 * @param content The content as a string
	 * @return The document
	 * @throws DfException The repository exception
	 */
	private IDfDocument createDocument(final String content)
		throws DfException {
		final IDfClientX clientx = new DfClientX();
		final IDfDocument doc = (IDfDocument) getSession().newObject(
																DM_DOCUMENT);
		final String objectName = UUID.randomUUID().toString();
		doc.setObjectName(objectName);
		doc.setContentType("crtext");
		doc.setContent(clientx.StringToByteArrayOutputStream(content));
		doc.save();

		DfLogger.info(this, "Created document [ {0} | {1}]",
					  new String[] {
					      doc.getObjectId().getId(),
					      doc.getObjectName()
					  }, null);
		
		return doc;
	}

	/**
	 * Quick test.
	 * @param args The command line args
	 */
	public static void main(String[] args) {
		if (args != null && args.length != 3) {
			System.out.println("DummyMethod <docbase> <username> <password>");
			System.exit(1);
		} else {
			final DctmCommons dc = DctmCommons.getInstance();
			try {
				dc.session.useSession(args[0], args[1], args[2], null,
									  false, new ISessionCallback() {
					@Override
					public void useSession(final IDfSession session)
					throws DfException {
						final int ret = dc.withMethod("dummy-method")
											   .useSessionCredentials()
											   .add("message", "Hello world")
											   .setTraceLevel(10)
											   .execute(session);
						System.out.println("Method completed (" + ret + ")");
					}
				});
			} catch (DfException e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
			System.exit(0);
		}
	}
}
